#################################################################################### 
####################################################################################
####################################################################################
# Appendix L: Figures L.7--L.9
####################################################################################
####################################################################################
library(here)
library(kableExtra)
library(lmtest)
library(multiwayvcov)
library(ggplot2)
####################################################################################
####################################################################################
## Read in survey data (from: power_dist_covaraites.R)
dat = readRDS(here("data/dat_unrestricted.rds"))

####################################################################################
####################################################################################
## Figures L.4
dat = dat[dat$treatment == 0,]

####################################################################################
# Partisanship
dat$share2006_bins3 = 0
dat[dat$share2006 > .6,]$share2006_bins3 = 1

d2 <- dat[, c("power_csk","power_csd","power_ngos","power_pres","power_mp","power_dc","power_lc", "share2006_bins3")] %>% 
  gather(., key = "key", value = "value", -one_of("share2006_bins3")) %>%
  group_by(key) %>%
  filter(value == 4) %>%
  group_by(key, share2006_bins3) %>%
  summarise(count=n()) 

d2$count = as.numeric(d2$count)
d2[d2$share2006_bins3 == 1,]$count = as.numeric( (d2[d2$share2006_bins3 == 1,]$count / nrow(dat[dat$share2006_bins3 == 1,])) )
d2[d2$share2006_bins3 == 0,]$count = as.numeric(d2[d2$share2006_bins3 == 0,]$count / nrow(dat[dat$share2006_bins3 == 0,]))

d2$share2006_bins3 = factor(d2$share2006_bins3, levels = c(0,1),
                            labels = c("Low Support", "High Support")) 

pres_power = ggplot(d2, aes(x = reorder(key, -count), y = count*100)) +
  geom_bar(stat="identity", position = "dodge") +
  theme_bw() + facet_wrap(~share2006_bins3) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, max(d2$count*100)+1), labels=function(x) paste0(x,"%")) +
  scale_x_discrete(labels= c("The\nPresident", "NGOs", "Members of\nParliament", "District\nCouncilors", "Civil Servants\nKampala", 
                             "Civil Servants\nDistrict", "Local\nCouncilors")) + 
  geom_text(aes(x = key, y = (count*100)-2.5), color="#000000", label = round(d2$count*100, 0), 
            angle=0, vjust = 0, parse = T,  size = 4, check_overlap = TRUE) + 
  labs(title = "Share of Respondents Reporting Actor Has \"A lot\" of Power\nOver NGO Project Locations",
       x = NULL,
       y = "Percent") + 
  theme(legend.title=element_blank(),
        plot.title = element_text(size=14,hjust = 0.5),
        legend.position = c(0.1, 0.9),
        legend.background = element_blank(),
        axis.text.x = element_text(size=7))

ggsave(file = here("plots/power_alot_partisanship_wrap.pdf"), pres_power, width = 10, height = 6)

####################################################################################
# Region

d2 <- dat[, c("power_csk","power_csd","power_ngos","power_pres","power_mp","power_dc","power_lc", "region")] %>% 
  gather(., key = "key", value = "value", -one_of("region")) %>%
  group_by(key) %>%
  filter(value == 4) %>%
  group_by(key, region) %>%
  summarise(count=n()) 

d2$count = as.numeric(d2$count)
d2[d2$region == 1,]$count = as.numeric( (d2[d2$region == 1,]$count / nrow(dat[dat$region == 1,])) )
d2[d2$region == 2,]$count = as.numeric( (d2[d2$region == 2,]$count / nrow(dat[dat$region == 2,])) )
d2[d2$region == 3,]$count = as.numeric( (d2[d2$region == 3,]$count / nrow(dat[dat$region == 3,])) )

d2$region = factor(d2$region, levels = c(1,2,3),
                   labels = c("East/Central", "North", "West")) 

pres_power = ggplot(d2, aes(x = reorder(key, -count), y = count*100)) +
  geom_bar(stat="identity", position = "dodge") +
  theme_bw() + facet_wrap(~region) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, max(d2$count*100)+1), labels=function(x) paste0(x,"%")) +
  scale_x_discrete(labels= c("The\nPresident", "NGOs", "Members of\nParliament", "District\nCouncilors", "Civil Servants\nKampala", 
                             "Civil Servants\nDistrict", "Local\nCouncilors")) + 
  geom_text(aes(x = key, y = (count*100)-2.5), color="#000000", label = round(d2$count*100, 0), 
            angle=0, vjust = 0, parse = T,  size = 4, check_overlap = TRUE) + 
  labs(title = "Share of Respondents Reporting Actor Has \"A lot\" of Power\nOver NGO Project Locations",
       x = NULL,
       y = "Percent") + 
  theme(legend.title=element_blank(),
        plot.title = element_text(size=14,hjust = 0.5),
        legend.position = c(0.1, 0.9),
        legend.background = element_blank(),
        axis.text.x = element_text(size=7))
ggsave(file = here("plots/power_alot_region_wrap.pdf"), pres_power, width = 10, height = 6)


####################################################################################
# Education

dat$education_level_bin = 0
dat[dat$education_level >= 3,]$education_level_bin = 1

d2 <- dat[, c("power_csk","power_csd","power_ngos","power_pres","power_mp","power_dc","power_lc", "education_level_bin")] %>% 
  gather(., key = "key", value = "value", -one_of("education_level_bin")) %>%
  group_by(key) %>%
  filter(value == 4) %>%
  group_by(key, education_level_bin) %>%
  summarise(count=n()) 

d2$count = as.numeric(d2$count)
d2[d2$education_level_bin == 1,]$count = as.numeric( (d2[d2$education_level_bin == 1,]$count / nrow(dat[dat$education_level_bin == 1,])) )
d2[d2$education_level_bin == 0,]$count = as.numeric(d2[d2$education_level_bin == 0,]$count / nrow(dat[dat$education_level_bin == 0,]))

d2$education_level_bin = factor(d2$education_level_bin, levels = c(0,1),
                                labels = c("Low Education", "High Education")) 

pres_power = ggplot(d2, aes(x = reorder(key, -count), y = count*100)) +
  geom_bar(stat="identity", position = "dodge") +
  theme_bw() + facet_wrap(~education_level_bin) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, max(d2$count*100)+1), labels=function(x) paste0(x,"%")) +
  scale_x_discrete(labels= c("The\nPresident", "NGOs", "Members of\nParliament", "District\nCouncilors", "Civil Servants\nKampala", 
                             "Civil Servants\nDistrict", "Local\nCouncilors")) + 
  geom_text(aes(x = key, y = (count*100)-2.5), color="#000000", label = round(d2$count*100, 0), 
            angle=0, vjust = 0, parse = T,  size = 4, check_overlap = TRUE) + 
  labs(title = "Share of Respondents Reporting Actor Has \"A lot\" of Power\nOver NGO Project Locations",
       x = NULL,
       y = "Percent") + 
  theme(legend.title=element_blank(),
        plot.title = element_text(size=14,hjust = 0.5),
        legend.position = c(0.1, 0.9),
        legend.background = element_blank(),
        axis.text.x = element_text(size=7))
ggsave(file = here("plots/power_alot_education_wrap.pdf"), pres_power, width = 10, height = 6)
